/*
* Copyright (c) 2024 SwanLink (Jiangsu) Technology Development Co., LTD.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_yfree/YFree.dart';
import 'package:flutter/material.dart';

import 'Localizations.dart';

class MyAlert extends YPage {
  var title = "";
  int counter = 0; // 初始计数值

  @override
  void yOnCreate() {
    setState(() => title = yGetAppName());
    yBroadcast_register("broadcastKey", (params) => yToast(params!["msg"]));
  }

  @override
  void initState() {
    super.initState();
    yBroadcast_send("broadcastKey", params: {"msg": "initState"});
  }

  @override
  void yOnDestory() {
    super.yOnDestory();
    yBroadcast_send("broadcastKey", params: {"msg": "yOnDestory"});
  }

  @override
  Future yOnYFreeCall(String method, Map arguments) {
    return super.yOnYFreeCall(method, arguments);
  }

  @override
  void yOnReceiveBroadcast(String broadCastName, Map arguments) {
    super.yOnReceiveBroadcast(broadCastName, arguments);
  }

  @override
  bool ySafeAreaBottom() {
    return false;
  }

  Future<void> showToast() async {
    yBroadcast_send("broadcastKey", params: {"msg": "欢迎使用YFree_Flutter"});
  }

  Widget view_button(String title, Function() onTap) {
    return YButton(
      child: Text(title,
          style: const TextStyle(
              color: Colors.white, fontSize: 20, fontWeight: FontWeight.bold)),
      padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
      color: YConfig.themeColor,
      onTap: onTap,
    );
  }

  @override
  Widget yBuild(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          YAppBar(title: title, canBack: false),
          Container(
            width: yScreenSize().width,
            padding: const EdgeInsets.all(5),
            child: Wrap(
              spacing: 5,
              runSpacing: 5,
              direction: Axis.vertical,
              children: [
                view_button('yShowDialog', () {
                  yShowDialog(
                    alertBuilder: (pageContext, dialogContext) {
                      return AlertDialog(
                        title: Text('Custom Dialog'),
                        content: Text('This is a custom dialog.'),
                        actions: [
                          TextButton(
                            onPressed: () {
                              yDismissDialog(dialogContext); // 关闭对话框
                            },
                            child: Text('yDismissDialog 关闭对话框'),
                          ),
                        ],
                      );
                    },
                    context: context, // 传递调用时的 BuildContext
                  );
                }),
                view_button('yShowStatefulDialog', () {
                  yShowStatefulDialog(
                    alertBuilder: (pageContext, dialogContext, statefulContext,
                        setDialogState) {
                      // 使用 StatefulBuilder 来管理对话框内的状态
                      return StatefulBuilder(
                        builder: (context, setState) {
                          return AlertDialog(
                            title: Text('Stateful Dialog'),
                            content: Column(
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text('yShowStatefulDialog Counter value: $counter'),
                                SizedBox(height: 16),
                                ElevatedButton(
                                  onPressed: () {
                                    setDialogState(() {
                                      // 更新对话框的状态
                                      counter++; // 更新计数器
                                    });
                                  },
                                  child: Text('Update value'),
                                ),
                              ],
                            ),
                            actions: [
                              TextButton(
                                onPressed: () {
                                  yDismissDialog_unDismissible(); // 关闭对话框
                                },
                                child: Text('unDismissible关闭对话框'),
                              ),
                            ],
                          );
                        },
                      );
                    },
                    dismissible: false, // 是否可以点击外部区域关闭对话框
                    context: context, // 传递调用时的 BuildContext
                  );
                }),
                view_button('yShowBuilder', () {
                  yShowBuilder("本项目仅可用于学习使用，非作者本人同意，请勿传播或用做任何商业用途。\n感谢您的支持！",
                      () => showToast(), null, false);
                }),
                view_button('yShowActionSheet', () {
                  yShowActionSheet('hello', ['aaa', 'bbb'],
                      (int index, String action) {
                    yBroadcast_send("broadcastKey", params: {
                      "msg":
                          'index :' + index.toString() + ', action : ' + action
                    });
                  }, onCancel: () {
                    yBroadcast_send("broadcastKey", params: {"msg": '取消操作'});
                  });
                }),
                view_button("切换语言".yLocale(), () {
                  final items = [null, zh, en];
                  yShowSelectBuilder("切换语言".yLocale(), true,
                      items.map((e) => getLanguage(e)).toList(),
                      (index, action) {
                    YLocaleUtils.updateAppLocale(items[index]);
                  });
                }),
              ],
            ),
          ),
        ],
      ),
    );
  }
}
